package app.itgungnir.compose.wan.api

import androidx.paging.PagingSource
import androidx.room.*
import app.itgungnir.compose.wan.business.main.agenda.Agenda
import app.itgungnir.compose.wan.business.main.home.HomeArticle
import app.itgungnir.compose.wan.business.main.home.HomeBanner
import app.itgungnir.compose.wan.business.main.square.Square
import app.itgungnir.compose.wan.business.my_collection.MyCollection
import app.itgungnir.compose.wan.business.my_website.MyWebsite
import kotlinx.coroutines.flow.Flow

@Dao
interface WanDao {

    // ================================================================
    // 首页轮播图
    // ================================================================
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertAllHomeBanners(items: List<HomeBanner>)

    @Query("DELETE FROM homebanner")
    suspend fun deleteAllHomeBanners()

    @Query("SELECT * FROM homebanner")
    fun queryHomeBanners(): Flow<List<HomeBanner>>

    // ================================================================
    // 首页文章列表
    // ================================================================
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertAllHomeArticles(items: List<HomeArticle>)

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertAllHomeHotArticles(items: List<HomeArticle>)

    @Query("DELETE FROM homearticle WHERE type = 0")
    suspend fun deleteAllHomeArticles()

    @Query("DELETE FROM homearticle WHERE type = 1")
    suspend fun deleteAllHomeHotArticles()

    @Query("UPDATE homearticle SET collect = :collect WHERE id = :id")
    suspend fun updateHomeArticleCollect(id: Long, collect: Boolean)

    @Query("UPDATE homearticle SET collect = 0")
    suspend fun updateAllHomeArticlesUncollected()

    @Query("SELECT * FROM homearticle WHERE type = 0 ORDER BY publishTime DESC")
    fun queryHomeArticles(): PagingSource<Int, HomeArticle>

    @Query("SELECT * FROM homearticle WHERE type = 1 ORDER BY publishTime DESC")
    fun queryHomeHotArticles(): Flow<List<HomeArticle>>

    @Query("SELECT COUNT(*) FROM homearticle WHERE id = :id AND collect = 1 ORDER BY publishTime DESC")
    fun queryHomeArticleCollectCount(id: Long): Flow<Int>

    // ================================================================
    // 我喜欢的文章
    // ================================================================
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertMyCollection(item: MyCollection)

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertAllMyCollections(items: List<MyCollection>)

    @Query("DELETE FROM mycollection WHERE id = :id")
    suspend fun deleteMyCollection(id: Long)

    @Query("DELETE FROM mycollection")
    suspend fun deleteAllMyCollections()

    @Update(onConflict = OnConflictStrategy.REPLACE)
    suspend fun updateMyCollection(item: MyCollection)

    @Query("SELECT * FROM mycollection ORDER BY publishTime DESC")
    fun queryMyCollections(): PagingSource<Int, MyCollection>

    // ================================================================
    // 我常用的网站
    // ================================================================
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertMyWebsite(item: MyWebsite)

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertAllMyWebsites(items: List<MyWebsite>)

    @Query("DELETE FROM mywebsite WHERE id = :id")
    suspend fun deleteMyWebsite(id: Long)

    @Query("DELETE FROM mywebsite")
    suspend fun deleteAllMyWebsites()

    @Update(onConflict = OnConflictStrategy.REPLACE)
    suspend fun updateMyWebsite(item: MyWebsite)

    @Query("SELECT * FROM mywebsite")
    fun queryMyWebsites(): Flow<List<MyWebsite>>

    // ================================================================
    // 我的待办
    // ================================================================
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertAgenda(item: Agenda)

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertAllAgendas(items: List<Agenda>)

    @Query("DELETE FROM agenda WHERE id = :id")
    suspend fun deleteAgenda(id: Long)

    @Query("DELETE FROM agenda")
    suspend fun deleteAllAgendas()

    @Update(onConflict = OnConflictStrategy.REPLACE)
    suspend fun updateAgenda(item: Agenda)

    @Query("SELECT * FROM agenda ORDER BY date DESC, id DESC")
    fun queryAgendasByDateDesc(): PagingSource<Int, Agenda>

    @Query("SELECT * FROM agenda ORDER BY date ASC, id DESC")
    fun queryAgendasByDateAsc(): PagingSource<Int, Agenda>

    @Query("SELECT * FROM agenda ORDER BY completeDate DESC, id DESC")
    fun queryAgendasByCompleteDateDesc(): PagingSource<Int, Agenda>

    @Query("SELECT * FROM agenda ORDER BY completeDate ASC, id DESC")
    fun queryAgendasByCompleteDateAsc(): PagingSource<Int, Agenda>

    // ================================================================
    // 我的广场文章
    // ================================================================
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertSquare(item: Square)

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertAllSquares(items: List<Square>)

    @Query("DELETE FROM square WHERE id = :id")
    suspend fun deleteSquare(id: Long)

    @Query("DELETE FROM square")
    suspend fun deleteAllSquares()

    @Query("SELECT * FROM square ORDER BY publishTime DESC")
    fun querySquares(): PagingSource<Int, Square>
}
